// pages/share/share.js
const app = getApp();
import {
  IncomeModels
} from "../../utils/models/income/Income-models";
let incomeModels = new IncomeModels;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: app.globalData.navbarHeight,
    height: '',
    titleName: '蒲尽广佛',
    navBgColor: "yellow",
    isLeft: false,
    showDel: false,
    searchVal: '',
    type: 0,
    sort : 'desc',
    shareList:[]
  },

  showDel: function() {
    this.setData({
      showDel : true
    });
  },

  hideDel: function() {
    this.setData({
      showDel : false
    });
  },

  getList: function (sort,type,search){
    let that = this;
    let param = {
      type: type,
      sort:sort,
      search:search
    }
    incomeModels.getShareList(param,function(res){
      if(res.code < 0){
        wx.navigateTo({
          url: './share_invite?invite_id=1',
        })
      }
      that.setData({
        shareList:res.data
      })
    })
  },

  // 排序
  getSort: function(e) {
    var type = e.currentTarget.dataset.type; //类型
    if(this.data.sort == 'asc') {
      var sort = 'desc';
    }else {
      var sort = 'asc';
    }
    let searchVal = this.data.searchVal;
    this.getList(sort, type, searchVal)
    this.setData({
      type: type,
      sort: sort,
    });
  },

  // 获取输入的搜素内容
  searchVal: function(e) {
    var searchVal = e.detail.value;
    this.setData({
      searchVal: searchVal,
      // showDel: false
    });
    let sort = this.data.sort;
    let type = this.data.type;
    this.getList(sort, type, searchVal);
  },

  // 删除搜索内容
  clearVal: function() {
    this.setData({
      searchVal: '',

    });
    let sort = this.data.sort;
    let type = this.data.type;
    let search = '';
    this.getList(sort, type, search);
  },
  onShareAppMessage: function( res ){
    console.log(res);
    var that = this;
    var gid = res.currentTarget.dataset.gid;
    var loginInfo = wx.getStorageSync('userinfo');
    var invite_id = 0;
　　// 设置菜单中的转发按钮触发转发事件时的转发内容
    if (loginInfo && loginInfo.user_id > 0) {
      invite_id = loginInfo.user_id;
    }

    //判断是否通过点击按钮分享
    if (res.from == 'button') {
      console.log(12333);
      console.log(res);
      return {
        path: '/pages/product/product_detail?invite_id=' + invite_id + '&gid='+gid
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let sort = this.data.sort;
    let type = this.data.type;
    let search = '';
    this.getList(sort, type, search);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})